package com.rfid.util;

import android.text.TextUtils;

import com.rfid.config.ConfigManager;
import com.rfid.jni.RfidDecoder;
import com.util.StringTool;

public class RfidUitl {

    private static RfidDecoder mRfidDecoder = new RfidDecoder();


    public static String decodeBookEpc(String epc) {
        try {
            String hex = epc.replace(" ", "").trim();
            System.out.println(hex);//E200001918190052195017B6崩溃,010200A118030000C4000000正常
            BeeperUtils.beep(BeeperUtils.BEEPER_SHORT);
            LogUtils.v("hex = " +hex);
            hex = hex.substring(4 * 2, hex.length());
            byte data[] = Util.hexStr2bytes(hex);
            int r = 0;
            System.out.println("data len = " + data.length);
            if (hex.length() > 16) {
                System.out.println("iRFID_Decode128bit");
                r = mRfidDecoder.iRFID_Decode128bit(data);
            } else {
                System.out.println("iRFID_Decode96bit");
                r = mRfidDecoder.iRFID_Decode96bit(data);
            }
            System.out.println("result = " + new String(data).trim() + "---" + r);
            if(r > 0) {
                return new String(data).trim();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String decodeLevelEpc(String epc) {
        try {
            String hex = epc.replace(" ", "").trim();
            System.out.println(hex);//E200001918190052195017B6崩溃,010200A118030000C4000000正常
            BeeperUtils.beep(BeeperUtils.BEEPER_SHORT);
            LogUtils.v("hex = " +hex);
            hex = hex.substring(0, hex.length()-4 * 2);
            byte data[] = Util.hexStr2bytes(hex);
            int r = 0;
            System.out.println("data len = " + data.length);
            if (hex.length() > 16) {
                System.out.println("iRFID_Decode128bit");
                r = mRfidDecoder.iRFID_Decode128bit(data);
            } else {
                System.out.println("iRFID_Decode96bit");
                r = mRfidDecoder.iRFID_Decode96bit(data);
            }
            System.out.println("result = " + new String(data).trim() + "---" + r);
            if(r > 0) {
                return new String(data).trim();
            }
        }catch (Exception e) {
            LogUtils.w(epc);
            e.printStackTrace();
        }
        return "";
    }

    public static String encodeLevelBarcode(String barcode) {
        byte data[] = barcode.trim().getBytes();
        byte buffer [] = new byte[24];
        System.arraycopy(data,0,buffer,0,data.length);
        int r = mRfidDecoder.iRFID_Encode96bit(buffer);
        LogUtils.v("encode96it r = " + r + "---" + Util.bytesToHexString(buffer).toUpperCase());
        if(r > 0)
            return Util.bytesToHexString(buffer).toUpperCase();
        else
            return "";
    }

    public static boolean isLevelBarcode(String barcode) {
        if(TextUtils.isEmpty(barcode)) {
            return false;
        }
        return barcode.endsWith(",");
    }
}
